package avicit.bdp.dms.das.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.service.dto.BdpColumnDTO;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.dms.das.dao.BdpDasColumnDao;
import avicit.bdp.dms.das.vo.ColumnInfoVO;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.core.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2020-06-24 14:54
 * @类说明：bdp_das_columnService
 * @修改记录：
 */
@Service
public class BdpDasColumnService extends BaseService<BdpDasColumnDao, BdpColumnDTO> {

    /**
     * 新增对象
     *
     * @param bdpDasColumnDTO 保存对象
     * @return String
     */
    @Transactional(rollbackFor = Exception.class)
    public String insertBdpDasColumn(BdpColumnDTO bdpDasColumnDTO) {
        try {
            if (StringUtils.isEmpty(bdpDasColumnDTO.getId())) {
                bdpDasColumnDTO.setId(ComUtil.getId());
            }
            insertSelective(bdpDasColumnDTO);
            //记录日志
            BdpLogUtil.log4Insert(bdpDasColumnDTO);
            return bdpDasColumnDTO.getId();
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    /**
     * 修改对象全部字段
     *
     * @param bdpDasColumnDTO 修改对象
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateBdpDasColumn(BdpColumnDTO bdpDasColumnDTO) {
        try {
            updateByPrimaryKey(getUpdateDto(bdpDasColumnDTO));
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }


    /**
     * 内部方法，获取修改的dto对象
     *
     * @param bdpDasColumnDTO dto对象
     * @return dto对象
     */
    private BdpColumnDTO getUpdateDto(BdpColumnDTO bdpDasColumnDTO) {
        BdpColumnDTO oldDTO = selectByPrimaryKey(bdpDasColumnDTO.getId());
        if (oldDTO == null) {
            throw new BusinessException("数据不存在");
        }
        //记录日志
        BdpLogUtil.log4Update(bdpDasColumnDTO, oldDTO);
        oldDTO.setName(bdpDasColumnDTO.getName());
        oldDTO.setCnName(bdpDasColumnDTO.getCnName());
        oldDTO.setRemark(bdpDasColumnDTO.getRemark());
        oldDTO.setColumnType(bdpDasColumnDTO.getColumnType());
        oldDTO.setColumnLength(bdpDasColumnDTO.getColumnLength());
        oldDTO.setColumnPrecision(bdpDasColumnDTO.getColumnPrecision());
        oldDTO.setNullable(bdpDasColumnDTO.getNullable());
        oldDTO.setPrimaryKey(bdpDasColumnDTO.getPrimaryKey());
        oldDTO.setUniqueKey(bdpDasColumnDTO.getUniqueKey());
        oldDTO.setForeignKey(bdpDasColumnDTO.getForeignKey());
        oldDTO.setStandardId(bdpDasColumnDTO.getStandardId());
        oldDTO.setOrderBy(bdpDasColumnDTO.getOrderBy());
        oldDTO.setNameOld(bdpDasColumnDTO.getNameOld());
        return oldDTO;
    }

    /**
     * 按主键单条删除
     *
     * @param id 主键id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteBdpDasColumnById(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("删除失败！传入的参数主键为null");
        }
        try {
            //记录日志
            BdpColumnDTO bdpDasColumnDTO = selectByPrimaryKey(id);
            if (bdpDasColumnDTO == null) {
                throw new BusinessException("删除失败！对象不存在");
            }
            BdpLogUtil.log4Delete(bdpDasColumnDTO);
            //删除业务数据
            deleteByPrimaryKey(id);
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    /**
     * 批量删除数据
     *
     * @param ids id的数组
     * @return int
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteBdpDasColumnByIds(String[] ids) {
        int result = 0;
        for (String id : ids) {
            deleteBdpDasColumnById(id);
            result++;
        }
        return result;
    }


    /**
     * 根据所属表id删除
     *
     * @param tableId 表id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteBdpDasColumnByTableId(String tableId) {
        if (StringUtils.isEmpty(tableId)) {
            throw new BusinessException("删除失败！传入的参数主键为null");
        }
        try {
            BdpColumnDTO bdpDasColumnDTO = new BdpColumnDTO();
            bdpDasColumnDTO.setTableId(tableId);
            delete(bdpDasColumnDTO);
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    public List<BdpColumnDTO> searchByTableId(List<String> tableIdList) {
        return mapper.selectColumnList(tableIdList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateColumnNameOldNull(String tableId) {
        this.mapper.updateColumnNameOldNull(tableId);
    }

    /**
     * 批量添加
     *
     * @param columnList 列集合
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertListByBatch(List<BdpColumnDTO> columnList){
        this.mapper.insertListByBatch(columnList);
    }

    /**
     * 通过tableId删除
     *
     * @param tableIdList 表ID集合
     */
    @Transactional(rollbackFor = Exception.class)
    void deleteByTableIdList(List<String> tableIdList) {
        this.mapper.deleteByTableIdList(tableIdList);
    }


    @Transactional(rollbackFor = Exception.class)
    void updateModifyAble(String tableId) {
        this.mapper.updateModifyAble(tableId);
    }

    public List<ColumnInfoVO> selectInfoColumns(List<String> tableQuery) {
        return this.mapper.selectInfoColumns(tableQuery);
    }

    public List<String> selectIdByTableId(String tableId) {
        return this.mapper.selectIdByTableId(tableId);
    }

    public List<BdpColumnDTO> selectIdByTableIds(List<String> tableIds) {
        return this.mapper.selectIdByTableIds(tableIds);
    }
}